197. Rising Temperature


Posted by ikl258794613 on 2024-02-22

Column Name Type
id int
recordDate date
temperature int

id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.

Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

The result format is in the following example.

Input:
Weather table:

id recordDate temperature
1 2015-01-01 10
2 2015-01-02 25
3 2015-01-03 20
4 2015-01-04 30

Output:

id
2
4

Explanation:
In 2015-01-02, the temperature was higher than the previous day (10 -> 25).
In 2015-01-04, the temperature was higher than the previous day (20 -> 30).

解答:

SELECT w1.id FROM Weather w1, Weather w2 
WHERE DATEDIFF(w1.recordDate,w2.recordDate) = 1 
AND w1.temperature > w2.temperature

學習點:
1.自己跟自己比較時SELECT自己兩次。
2.DATEDIFF ( datepart , startdate , enddate ) datepart不傳時返回兩個日期之間的差異。


#SQL







Related Posts

[Py 百日馬 Day 4-1] random module 常見方法 - 產生隨機數與亂數取樣/排序

[Py 百日馬 Day 4-1] random module 常見方法 - 產生隨機數與亂數取樣/排序

Day07 LIFF (LINE Front-end Framework)

Day07 LIFF (LINE Front-end Framework)

DE2_115(DAY4)以niosii去控制板子上的LCD panel

DE2_115(DAY4)以niosii去控制板子上的LCD panel


Comments